package com.jin.spirng_boot_learn.controllers;

import com.jin.spirng_boot_learn.RunForever;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@RestController
@RequestMapping("/test")
public class TestCacheController {


    @Cacheable(cacheNames = "allAreasCache", key = "#p0")
    @GetMapping("/cacheble")
    public String CachebleController(@RequestParam(name="name") String name) {
        try {
            Thread.sleep(1000 * 5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "cahceable|" + name;
    }

    @CachePut(cacheNames = "allAreasCache", key = "#p0")
    @GetMapping("/cacheput")
    public String CacheputController(@RequestParam(name="name") String name) {
        return "cacheput|" + name;
    }


    @CacheEvict(cacheNames = "allAreasCache", key = "#p0")
    @GetMapping("/cacheevict")
    public String CacheEvictController(@RequestParam(name="name") String name) {
        return "cacheevict cancell |" + name;
    }


    @Resource
    private RunForever.TaskService taskService;
    @GetMapping("/anysc")
    public String TestAnyscController () throws Exception {
        taskService.doTaskOne();
        taskService.doTaskTwo();
        return "cahceable|";
    }


    @PostMapping("/file")
    public String handleFileUpload(@RequestParam(name = "file") MultipartFile file) {

        System.out.println("sssss");
        System.out.println(file);
        return "redirect:/";
    }




}
